package com.util.framework;


import com.util.framework.filter.Filter;

import java.lang.reflect.Method;

/***
 * 类描述: 委托类
 *
 * @author FrodJiang
 * @date 18/03/2017
 */
public class Delegate {
    private static Method getMethod(Filter filter) throws DelegateException {
        Method[] methods = filter.getClass().getDeclaredMethods();
        if (methods.length == 0) {
            throw new DelegateException("过滤器错误!");
        }
        Method method = methods[0];
        method.setAccessible(true);
        return method;
    }

    public static Object invoke(Filter filter, Object target) throws DelegateException {
        try {
            return getMethod(filter).invoke(filter, target);
        } catch (Exception e) {
            throw new DelegateException(e);
        }
    }
}
